﻿// Copyright (c) MudBlazor 2021
// MudBlazor licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace MXERP.Client.Services;

public interface IUserPreferenceService
{
    /// <summary>
    ///     Saves UserPreferences in local storage
    /// </summary>
    /// <param name="userPreferences">The userPreferences to save in the local storage</param>
    public Task SaveUserPreference(UserPreference userPreference);

    /// <summary>
    ///     Loads UserPreferences in local storage
    /// </summary>
    /// <returns>UserPreferences object. Null when no settings were found.</returns>
    public Task<UserPreference?> LoadUserPreference();
}

public class UserPreferenceService : IUserPreferenceService
{
    private const string Key = "userPreferences";
    private readonly ILocalStorageService _localStorage;

    public UserPreferenceService(ILocalStorageService localStorage)
    {
        _localStorage = localStorage;
    }

    public async Task SaveUserPreference(UserPreference userPreference)
    {
        await _localStorage.SetItemAsync(Key, userPreference);
    }

    public async Task<UserPreference?> LoadUserPreference()
    {
        return await _localStorage.GetItemAsync<UserPreference>(Key);
    }
}